package cn.szzsi.business.controller.admin;


import cn.szzsi.core.file.Files;
import cn.szzsi.core.kit.CommonUtil;
import com.jfinal.core.Controller;
import com.jfinal.kit.Ret;

import java.util.List;

/**
 * Created by admin on 2017/4/24.
 */
public class ImgMaterialController extends BaseAdminController<Files>{
    private Files files;
    private String shopId;
    public void list(){
        render(CommonUtil.TEMPLATE_PATH+"/business/imgmaterial/img_material_list.html");
    }
    public void ajaxlist(){
        shopId=getAttr("shopId");
        List<Files> files = Files.dao.use(CommonUtil.ZSHOP_FILE).find("select * from sys_files where shopId=? and type=1 order by createDate desc",shopId);
        renderJson(files);
    }

    public void add(){
        shopId=getAttr("shopId");
        setAttr("shopId",shopId);
        render(CommonUtil.TEMPLATE_PATH+"/business/imgmaterial/img_material_input.html");
    }
    public void save(){
        shopId=getAttr("shopId");
        files = getModel(Files.class);
        Files originFile=Files.dao.getByShopIdAndTypeAndUrl(shopId,1,files.getUrl());
        if(originFile==null){
            renderJson(Ret.fail("info","请选择图片!"));
            return;
        }
        originFile.setOriginalName(files.getOriginalName());
        originFile.update();
        renderJson(Ret.ok("info","添加成功!").set("url","list"));
    }
    public void edit(){
        shopId=getAttr("shopId");
        String id = getPara("id","");
        files=Files.dao.use(CommonUtil.ZSHOP_FILE).findById(id);
        setAttr("shopId",shopId);
        setAttr("files",files);
        render(CommonUtil.TEMPLATE_PATH+"/business/imgmaterial/img_material_input.html");
    }
    public void update(){
        files = getModel(Files.class);
        files.use(CommonUtil.ZSHOP_FILE).update();
        renderJson(Ret.ok("info","修改成功!").set("url","list"));
    }

    public void delete(){
        ids = getParaValues("ids");
        Files.dao.use(CommonUtil.ZSHOP_FILE).deleteById(ids);
        renderJson(Ret.ok("info","删除成功!"));
    }

}
